package com.itboye.ihomebank.activity.electric_meter.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.LayoutRes;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.bean.ElectricMeterBean;
import com.itboye.ihomebank.bean.MeterDetailBean;
import com.itboye.ihomebank.widget.BatteryView;

import java.text.DecimalFormat;
import java.util.List;
import java.util.zip.Inflater;

/**
 * Created by Administrator on 2018/8/31 0031.
 */

public class AddressListAdapter extends BaseAdapter {
    private Context context;
    private List<MeterDetailBean> meterBeans;

    public AddressListAdapter(Context context,List<MeterDetailBean> meterBeans){
        this.context=context;
        this.meterBeans=meterBeans;

    }
    @Override
    public int getCount() {
//        return 4;
        return meterBeans.size();
    }

    @Override
    public Object getItem(int position) {
        return meterBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView==null){
           viewHolder=new ViewHolder();
           convertView=LayoutInflater.from(context).inflate(R.layout.item_electric_address,null,false);
           viewHolder.restMoney= (TextView) convertView.findViewById(R.id.restMoney);
           viewHolder.batteryView= (BatteryView) convertView.findViewById(R.id.batteryView);
           viewHolder.addressTv= (TextView) convertView.findViewById(R.id.addressTv);
           viewHolder.electric_status= (TextView) convertView.findViewById(R.id.electric_status);
           viewHolder.eleNum = (TextView) convertView.findViewById(R.id.eleNum);
            convertView.setTag(viewHolder);
        }else {
           viewHolder= (ViewHolder) convertView.getTag();

        }

        DecimalFormat df;
        if(Double.parseDouble(meterBeans.get(position).getBalance())/100<1){
            df = new DecimalFormat("#0.00");
        }else {
            df = new DecimalFormat("#.00");
        }

         viewHolder.restMoney.setText(df.format(Double.parseDouble(meterBeans.get(position).getBalance())/100)+"元");
         if (Double.parseDouble(meterBeans.get(position).getValue_remain())<0){
             viewHolder.batteryView.setPower(0);
         }else
        if (Double.parseDouble(meterBeans.get(position).getValue_remain())<1){ //进位取整
            int ceil = (int) Math.ceil(Double.parseDouble(meterBeans.get(position).getValue_remain()));
            viewHolder.batteryView.setPower(ceil);

            Log.d("====emeter",ceil+"");
        }else { //去掉小数取整
            int floor = (int) Math.floor(Double.parseDouble(meterBeans.get(position).getValue_remain()));
            viewHolder.batteryView.setPower(floor);
            Log.d("====emeter",floor+"");
        }

        if (Double.parseDouble(meterBeans.get(position).getValue_remain())<20){ //变红
            viewHolder.batteryView.setColor(Color.RED);
            viewHolder.eleNum.setTextColor(context.getResources().getColor(R.color.text_black));
        }
//        else if (Double.parseDouble(meterBeans.get(position).getValue_remain())<50){ // BLUE
//            viewHolder.batteryView.setColor(Color.BLUE);
//            viewHolder.eleNum.setTextColor(Color.BLUE);
//        }
        else { //homegreen
            viewHolder.batteryView.setColor(context.getResources().getColor(R.color.homegreen));
            viewHolder.eleNum.setTextColor(context.getResources().getColor(R.color.text_black));
        }
            int floor = (int) Math.floor(Double.parseDouble(meterBeans.get(position).getValue_remain()));
            viewHolder.eleNum.setText(floor+"");
            viewHolder.addressTv.setText(meterBeans.get(position).getAddress());

//           if (meterBeans.get(position).getMonitorStatus().equals("")){
//               viewHolder.electric_status.setText("已断闸");
//           }else {
//               viewHolder.electric_status.setVisibility(View.INVISIBLE);
//           }
        return convertView;
    }

    public void  addData(List<MeterDetailBean> meterBeans){
        this.meterBeans=meterBeans;
        this.notifyDataSetChanged();
        Log.d("=======",meterBeans.toString());
    }
    class ViewHolder{
     TextView restMoney; //剩余电费
     BatteryView  batteryView; //剩余电量
     TextView addressTv; //电表地址
     TextView electric_status; //电表状态  开或关
     TextView eleNum; //电量数值
  }

}
